﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{

    public partial class AddNewSponsor : System.Web.UI.Page
    {

        private string uploadDirectory;

        protected void Page_Load(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            OnlineUser ou = (OnlineUser)Session["User"];

            if (ou.UserAccessLevel != "5")
                Response.Redirect("~/Default.aspx");
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            this.PriorityDropDownList.SelectedValue = "1";
            //place files in website subfolder named SponsorPictures
            uploadDirectory = Path.Combine(Request.PhysicalApplicationPath, "SponsorPictures");

        }

        protected void AddSponsorButton_Click(object sender, EventArgs e)
        {
            bool IsGoodInput = true;
            this.ErrorLabel.Text = "";
            String SponsorName = this.SponsorNameTextBox.Text.Trim();
            String Link = this.LinkTextBox.Text.Trim();
            String ContractsLimit = this.SponsorContactsLimitTextBox.Text.Trim();
            String Priority = this.PriorityDropDownList.SelectedValue;
            if (SponsorName == "" || Link == "" || ContractsLimit == "")
            {
                IsGoodInput = false;
                this.ErrorLabel.Text = "Please fill all the fields";
            }
            //ErrorLabel.Text = Priority;
            //using this code, the saved file will reurain it's original  file name when it's placed on the server.
            string ServerfileName = Path.GetFileName(SponsorFileUpload.PostedFile.FileName);
            string FullUploadPath = Path.Combine(uploadDirectory, ServerfileName);
            string ImagePath = "~/SponsorPictures/" + ServerfileName;

            if (SponsorFileUpload.PostedFile.FileName == "")
            {
                this.ErrorLabel.Text = "No file specified";
                IsGoodInput = false;
            }
            else
            {
                //check the extension
                string extension = Path.GetExtension(SponsorFileUpload.PostedFile.FileName);
                switch (extension.ToLower())
                {
                    case ".bmp":
                    case ".gif":
                    case ".jpg":
                        break;
                    default:
                        //this.ErrorLabel.Text = "This file type is not allowed";
                        IsGoodInput = false;
                        return;
                }


            }
            try
            {
                SponsorFileUpload.PostedFile.SaveAs(FullUploadPath);
                //ErrorLabel.Text = "File " + ServerfileName;
                //     ErrorLabel.Text += " uploaded successfully to";
                //ErrorLabel.Text += FullUploadPath;
                //ErrorLabel.Text = FullUploadPath;
            }
            catch (Exception err)
            {
                this.ErrorLabel.Text = err.Message;
                IsGoodInput = false;
            }

            if (IsGoodInput)
            {
                string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
                System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
                dbCommand.CommandText = "Nivdal_AddNewSponsor";
                dbCommand.CommandType = CommandType.StoredProcedure;
                dbCommand.Connection = dbConnection;

                SqlParameter dbParam_SponsorName = new SqlParameter();
                dbParam_SponsorName.ParameterName = "@Name";
                dbParam_SponsorName.Value = SponsorName;
                dbParam_SponsorName.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_SponsorName);

                SqlParameter dbParam_SponsorImageFileName = new SqlParameter();
                dbParam_SponsorImageFileName.ParameterName = "@ImageFileName";
                dbParam_SponsorImageFileName.Value = ImagePath;
                dbParam_SponsorImageFileName.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_SponsorImageFileName);

                SqlParameter dbParam_SponsorLink = new SqlParameter();
                dbParam_SponsorLink.ParameterName = "@Link";
                dbParam_SponsorLink.Value = Link;
                dbParam_SponsorLink.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_SponsorLink);

                SqlParameter dbParam_SponsorContractsLimit = new SqlParameter();
                dbParam_SponsorContractsLimit.ParameterName = "@ContractsLimit";
                dbParam_SponsorContractsLimit.Value = ContractsLimit;
                dbParam_SponsorContractsLimit.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_SponsorContractsLimit);


                SqlParameter dbParam_SponsorPriority = new SqlParameter();
                dbParam_SponsorPriority.ParameterName = "@Priority";
                dbParam_SponsorPriority.Value = Priority;
                dbParam_SponsorPriority.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_SponsorPriority);

                SqlParameter dbParam_SponsorId = new SqlParameter();
                dbParam_SponsorId.ParameterName = "@SponsorId";
                dbParam_SponsorId.Value = "";
                dbParam_SponsorId.DbType = DbType.String;
                dbParam_SponsorId.Direction = ParameterDirection.Output;
                dbParam_SponsorId.Size = 64;
                dbCommand.Parameters.Add(dbParam_SponsorId);

                try
                {

                    dbConnection.Open();
                    dbCommand.ExecuteScalar();
                    //Response.Redirect("Sponsor.aspx?SponsorId="+dbParam_SponsorId.Value.ToString());
                }
                catch (Exception SQLDBException)
                {
                    this.ErrorLabel.Text = Convert.ToString(SQLDBException);
                    return;

                }

                finally
                {
                    dbConnection.Close();
                }

                try
                {
                    SponsorFileUpload.PostedFile.SaveAs(FullUploadPath);
                    //ErrorLabel.Text = "File " + ServerfileName;
                    //     ErrorLabel.Text += " uploaded successfully to";
                    //ErrorLabel.Text += FullUploadPath;
                    //ErrorLabel.Text = FullUploadPath;
                }
                catch (Exception err)
                {
                    this.ErrorLabel.Text = err.Message;
                }
            }
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}